//
//  FootballViewController.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/7/17.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "FootballViewController.h"

@interface FootballViewController ()<UIWebViewDelegate>
    {
        UIButton *backBtn;
    }

@property (nonatomic,strong) UIWebView * webViewShow;
@property (nonatomic,strong) UIActivityIndicatorView * activityIndicator;

@end

@implementation FootballViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    [self setStatusBarBackgroundColor:DEF_COLOR_MAINCOLOR];
}
- (void)setStatusBarBackgroundColor:(UIColor *)color {
    
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)]) {
        statusBar.backgroundColor = color;
    }
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];

}

-(UIWebView *) webViewShow{
    if (!_webViewShow) {
        _webViewShow = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, DEF_DEVICE_WIDTH, DEF_DEVICE_HEIGHT)];
        [_webViewShow setDelegate:self];
        [self.view addSubview:_webViewShow];
        _webViewShow.scrollView.bounces=NO;
    }
    return _webViewShow;
}
-(UIActivityIndicatorView *) activityIndicator{
    
    if (!_activityIndicator) {
        _activityIndicator = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
        _activityIndicator.center = CGPointMake(DEF_DEVICE_WIDTH/2, DEF_DEVICE_HEIGHT/2);
        _activityIndicator .backgroundColor =[UIColor grayColor];
        
        _activityIndicator.layer.cornerRadius = 5;
        _activityIndicator.layer.masksToBounds = YES;
        [_activityIndicator startAnimating];
        
        [self.view addSubview:_activityIndicator];
    }
    return _activityIndicator;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self.webViewShow loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://live.159cai.com/live/jingcai?from=159cai"]cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:15]];
    
    [self backBUtton];
}

-(void) backBUtton{
    NSString * title=nil;
    //设置返回按钮
    UIImage *nmlImg = [UIImage imageNamed:@"cion_back"];
    
    CGRect  rect;

    if (DEF_DEVICE_HEIGHT ==  812) {
        
        rect = CGRectMake(15, 44 + 5, 30, 30);
    }else{
        
        rect = CGRectMake(15, 30, 30, 30);
    }
    
    backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [backBtn setFrame:rect];
    [backBtn setImage:nmlImg forState:UIControlStateNormal];
    [backBtn setImage:[UIImage imageNamed:@"cion_back"] forState:UIControlStateHighlighted];
    [backBtn setTitle:title forState:UIControlStateNormal];
    
    //设置字体大小
    UIFont *titleFont = [UIFont systemFontOfSize:15];
    backBtn.titleLabel.font = titleFont;
    
    //设置字体颜色
    [backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    
    
    [self.view addSubview:backBtn];
    
    //监听事件
    [backBtn addTarget:self action:@selector(backClick) forControlEvents:UIControlEventTouchUpInside];
}
-(void) backClick{
    
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    [self.navigationController popToRootViewControllerAnimated:YES];
}
//当web开始加载
- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    //关开启指示器
    [self.activityIndicator startAnimating];
}
//当web完成加载
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
#pragma 得到网站的标题
    self.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
    
    [self.activityIndicator stopAnimating];
    
    [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByClassName('ui-toolbar-wrap')[0].style.backgroundColor='#FA223A';document.getElementsByClassName('ui-toolbar-wrap')[0].style.border='#FA223A'"];

    
}
//当web加载失败
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    //关闭指示器
    [self.activityIndicator stopAnimating];
}
    
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    NSString *url=[NSString stringWithFormat:@"%@",request.URL];
    
    if ([url hasPrefix:@"http://live.159cai.com/odds"]) {
        
        [backBtn setHidden:YES];
    }else{
        [backBtn setHidden:NO];
    }
    return YES;
   
}
- (void)dealloc
{
    self.webViewShow.delegate = nil;
    [self.webViewShow loadHTMLString: @"" baseURL: nil];
    [self.webViewShow stopLoading];
    [self.webViewShow removeFromSuperview];
    [[NSURLCache sharedURLCache] removeAllCachedResponses];
}


//点击返回的方法
- (void)backNative
{
    //判断是否有上一层H5页面
    if ([self.webViewShow canGoBack]) {
        //如果有则返回
        [self.webViewShow goBack];
        //同时设置返回按钮和关闭按钮为导航栏左边的按钮
        //        self.navigationItem.leftBarButtonItems = @[self.backItem, self.closeItem];
    } else {
        [self closeNative];
    }
}

//关闭H5页面，直接回到原生页面
- (void)closeNative
{
    [self.navigationController popViewControllerAnimated:YES];
}


- (UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
